# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class LgwPipeline:
    def process_item(self, item, spider):
        return item



# {'product_category': '雷公王锂电池',
#  'product_feature': [{'电压：': '12V'},
#                      {'产品重量：': '0.16kg/0.2kg'},
#                      {'外壳材质：': '塑料'},
#                      {'产品尺寸：': '105*63*34mm'},
#                      {'装箱数量：': '50'},
#                      {'电池规格容量：': '1500/2600毫安'}],
#  'product_name': '锂电池CR-7.4V2000毫安锂电池',
#  'product_price': 'None',
#  'product_url': 'http://www.dahou2010.com/product/470.html'}

import pymongo
from .items import ProductItem

class MongoDBPipeline(object):
    def __init__(self):
        self.client = None
        self.db = None

    @classmethod
    def from_crawler(cls, crawler):
        cls.connection_string = crawler.settings.get('MONGODB_CONNECTION_STRING')
        cls.database = crawler.settings.get('MONGODB_DATABASE')
        cls.collection = crawler.settings.get('MONGODB_COLLECTION')
        return cls()

    def open_spider(self, _spider):
        self.client = pymongo.MongoClient(self.connection_string)
        self.db = self.client[self.database]

    def process_item(self, item, _spider):
        self.db[self.collection].update_one({
            'name': item['name']
        }, {
            '$set': dict(item)
        }, True)
        return item
    
    def close_spider(self, _spider):
        self.client.close()